﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using YArchitech.Revit;
using YArchitech.Revit.Electric.Common;

namespace Model
{
	public class CableTrayCode : HYCmdEntry
	{
		public CableTrayCode(ExternalCommandData commandData) : base(commandData)
		{
		}

		public bool Excute()
		{
			bool flag = false;
			bool result;
			using (DlgCableTrayCode dlgCableTrayCode = new DlgCableTrayCode())
			{
				dlgCableTrayCode.Show(WindowHelper.HWndRevit);
				WindowHelper.SendMessageToWndRevit();
				string b = "";
				for (;;)
				{
					Transaction transaction = new Transaction(this.m_doc, "CableTrayCode");
					transaction.Start();
					try
					{
						if (flag && dlgCableTrayCode.chkFormatCode.Checked && dlgCableTrayCode.chkDigitalCode.Checked)
						{
							string text = dlgCableTrayCode.txtDigitalCode.Text.Trim();
							if (text == b)
							{
								int num = Convert.ToInt32(text);
								dlgCableTrayCode.txtDigitalCode.Text = (num + 1).ToString();
							}
						}
						Reference reference = this.m_sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, new RevitFilter.SelectionCableTrayFilter(), "请选择要编码的桥架");
						CableTray cableTray = this.m_doc.GetElement(reference) as CableTray;
						Common.AddCableTrayParam(this.m_doc, cableTray);
						string text2 = dlgCableTrayCode.txtCableTrayCode.Text.Trim();
						b = dlgCableTrayCode.txtDigitalCode.Text.Trim();
						if (dlgCableTrayCode.chkFormatCode.Checked && dlgCableTrayCode.chkLevel.Checked)
						{
							string value = cableTray.GetParameter(BuiltInParameter.RBS_START_LEVEL_PARAM).AsValueString();
							int startIndex = text2.IndexOf("?");
							text2 = text2.Remove(startIndex, 1).Insert(startIndex, value);
						}
						if (dlgCableTrayCode.chkFormatCode.Checked && dlgCableTrayCode.chkDirection.Checked)
						{
							int startIndex2 = text2.LastIndexOf("?");
							text2 = text2.Remove(startIndex2, 1).Insert(startIndex2, this.GetCableTrayDirection(cableTray));
						}
						if (this.CodeIsRepeat(this.m_doc, text2))
						{
							TaskDialog.Show("盈建科提示", "项目中已有桥架使用了该编码。");
							dlgCableTrayCode.txtCableTrayCode.Focus();
						}
						else
						{
							cableTray.GetParameter("编号").Set(text2);
						}
						transaction.Commit();
						flag = true;
					}
					catch (Autodesk.Revit.Exceptions.OperationCanceledException)
					{
						dlgCableTrayCode.Close();
						transaction.Commit();
						result = true;
						break;
					}
					catch (Exception)
					{
						transaction.RollBack();
						dlgCableTrayCode.Close();
						result = false;
						break;
					}
				}
			}
			return result;
		}

		private bool CodeIsRepeat(Document doc, string code)
		{
			return ((ICollection<Element>)RevitFilter.GetElementsOfType(doc, typeof(CableTray)).ToElements().Where(delegate(Element r)
			{
				string text = r.GetParameter("编号").AsString();
				return !string.IsNullOrEmpty(text) && text == code;
			}).ToList<Element>()).Count != 0;
		}

		private string GetCableTrayDirection(CableTray ct)
		{
			HYCableTray<CableTray> hycableTray = new HYCableTray<CableTray>(ct);
			if (hycableTray.IsXAxis())
			{
				return "X";
			}
			if (hycableTray.IsYAxis())
			{
				return "Y";
			}
			if (hycableTray.IsZAxis())
			{
				return "Z";
			}
			return "T";
		}
	}
}
